﻿using System;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class SkillEffective_Buff : SkillEffective
    {
        protected override ISerializeCustomPOD OnExecute(SkillEffectiveData data, Unit target, SkillEffectiveContext context, int effectiveIndex, out SkillEffectiveResponse startResp)
        {
            startResp = null;
            if (data.effectiveConfig.customConfig is SkillEffective_Buff_ConfigData rConfig)
            {
                var rBuffID = rConfig.buffID;
                var rStack = rConfig.stack;
                var rAttacker = context.skill.executer;
                var nBuffID = BattleUtil.GetValue(rBuffID, rAttacker, target, 0);
                var nStack = BattleUtil.GetValue(rStack, rAttacker, target);
                var nParam = BattleUtil.GetValue(rConfig.param, rAttacker, target);
                var bAdd = !rConfig.remove;

                return this.Add(rAttacker, target, nBuffID, nStack, bAdd, nParam);
            }

            return null;
        }

        protected override ISerializeCustomPOD OnActionExecute(SkillEffectiveData data, SkillActionNode action, Unit target, ref SkillEffectiveContext context, int effectiveIndex)
        {
            if (action.customConfig is SkillEffectiveAction_Buff_ConfigData rConfig)
            {
                var rAttacker = context.skill.executer;
                var nBuffID = rConfig.buffID.GetValue(rAttacker, target, context).ToInt();
                var nStack = rConfig.stack?.GetValue(rAttacker, target, context).ToInt() ?? 1;
                var nParam = rConfig.param?.GetValue(rAttacker, target, context).ToInt() ?? 0;
                var bAdd = !rConfig.remove;

                return this.Add(rAttacker, target, nBuffID, nStack, bAdd, nParam);
            }

            return null;
        }

        private BuffData Add(Unit rAttacker, Unit rTarget, int nBuffID, int nStack, bool bAdd, int nParam)
        {
            var rMakerComp = rAttacker.GetComponent<BuffComponent>(ETComponentType.BuffComponent);
            var rTargetComp = rTarget.GetComponent<BuffComponent>(ETComponentType.BuffComponent);
            BuffHandler rBuffHandler;
            if (bAdd)
            {
                rBuffHandler = BuffController.Add(rMakerComp, rTargetComp, nBuffID, nStack, nParam);
                return BuffData.Create(rBuffHandler);
            }
            else
            {
                BuffController.Remove(rTargetComp, nBuffID, nStack);
            }

            return null;
        }

#if UNITY_EDITOR
        public override string ToString(SkillCustomConfig config)
        {
            var rData = config as SkillEffective_Buff_ConfigData;
            return $"{(rData.remove ? "移除" : "添加")}buff {(rData.buffID.isFormula ? "公式" : "")} {rData.buffID.value} 层数 " +
                   (rData.stack != null ? $"{(rData.stack.isFormula ? "公式" : "")} {rData.stack.value})" : "1");
        }
#endif
    }
}